/**
    @project {
        "templateId": "org.helgoboss.custom",
        "version": "0.1-SNAPSHOT",
        "dependencies": [
            "org.scala-lang:scala-library:2.9.0-1",
            "org.helgoboss:scala-additions:0.1-SNAPSHOT",
            "org.helgoboss:osgi-additions:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-api:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-convenience:0.1-SNAPSHOT",
            "org.helgoboss:bundle-project-facade:0.1-SNAPSHOT",
            "org.helgoboss:one-filer:0.1-SNAPSHOT"
        ],
        "commandletId": "org.helgoboss.build-helgoboss-commons",
        "deployment": {
            "obrRepositories": [
                "file:/D:/m2_repo/repository.xml"
            ],
            "obrResources": [
                "org.helgoboss.one-filer-ref-impl-activator:0.1-SNAPSHOT",
                "org.helgoboss.common-one-filer-templates-activator:0.1-SNAPSHOT"
            ]
        }
    }
*/

package org.helgoboss.build_helgoboss_commons_commandlet

import org.helgoboss.commandlet._
import org.helgoboss.osgi_additions._
import org.helgoboss.one_filer.OneFiler
import org.helgoboss.bundle_project_facade.BundleProjectFacade
import scala.xml.PrettyPrinter

class Activator extends OsgiProvider with OsgiServiceWatcher {
    whenBundleActive {
        whenServicePresent[OneFiler] { oneFiler =>
            new BuildHelgobossCommonsCommandlet(oneFiler).providesService[Commandlet]
        }
    }
}

import org.helgoboss.scala_additions.Implicits._

class BuildHelgobossCommonsCommandlet(oneFiler: OneFiler) extends Commandlet {
    val id = "org.helgoboss.build-helgoboss-commons"
    
    private val bundleProjectFacade = new BundleProjectFacade

    def execute(context: ExecutionContext) = {
        import org.helgoboss.commandlet_convenience.Implicits._
        val options = context.arguments.options
        val out = context.writer
        
        
        // Build one-filers
        val sourceDir = context.resolveFile("one-filers/trunk/src/main/one-filers")
        val targetDir = context.resolveFile("one-filers/trunk/target")
        targetDir.deleteRecursively
        val projectDatas = oneFiler.extractProjectDatas(sourceDir)
        bundleProjectFacade.generateBundleProjects(projectDatas, targetDir)
        
        // Collect all maven projects and build reactor pom
        val versions = options.qualified.getOrElse("versions", "trunks").toString
        val root = context.workingDirectory
        val projects = versions match {
            case "trunks" => 
                root.tree.include("*/trunk").exclude("one-filers/trunk", "helgoboss-obr-builder/**", "clean-local-obr-repository/**", "user-notification/**").filter(_.file.tree.exists(_.path matches "pom.xml")).map(_.path)
            
            case "tags" => 
                root.tree.include("*/tags").flatMap { node =>
                    node.file.tree(walkOrder = Some(_.getName > _.getName)).
                            exclude(".svn", "*/**", "").map(node.path / _.path).take(1)
                }
        }

        val pom = <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
            <modelVersion>4.0.0</modelVersion>
            
            <parent>
                <groupId>org.helgoboss</groupId>
                <artifactId>helgoboss-parent</artifactId>
                <version>0.4</version>
            </parent>
            <artifactId>helgoboss-commons-parent</artifactId>
            <version>0.1-SNAPSHOT</version>
            <name>Just an aggregator for all the helgoboss-commons projects. Is not supposed to be a parent project.</name>
            <packaging>pom</packaging>
            <modules>
                {
                    projects.map { path =>
                        <module>{path.components.mkString("/")}</module>
                    }
                }
                <module>one-filers/trunk/target</module>
            </modules>
        </project>

        context.resolveFile("pom.xml").content = new PrettyPrinter(80, 4).format(pom)
        0
    }
}